using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Options;

namespace 国际化和本地化
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            //添加本地化服务
            services.AddLocalization(options =>
            {
                options.ResourcesPath = "Resources"; //设置资源文件路径
            });


            //设置国际化的文化
            services.Configure<RequestLocalizationOptions>(options =>
            {
                //所支持的文化放在集合中
                var supportedCultures = new List<CultureInfo>()
                {
                    new CultureInfo("zh-CN"), //中国
                    new CultureInfo("en-US"), //美国
                    new CultureInfo("en-GB")  //英国
                };

                //设置默认的请求文化
                options.DefaultRequestCulture = new RequestCulture("zh-CN");
                //设置所支持的文化
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
            });

            //AddViewLocalization:添加视图的本地化
            services.AddControllersWithViews().AddViewLocalization();

           

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();

            app.UseRouting();

            app.UseRequestLocalization(app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
